﻿using System.Collections.ObjectModel;
using System.Drawing;
using System.Windows;
using System.Windows.Controls;
using VisionDesigner;
using VM.Common.Enums;

namespace VM.Start.Controls
{

    /// <summary>
    /// VMDisplayControl.xaml 的交互逻辑
    /// </summary>
    public partial class VMDisplayControl : UserControl
    {
        #region 依赖属性
        // 输入图像
        public static readonly DependencyProperty ImageProperty =
            DependencyProperty.Register(
                "Image", typeof(CMvdImage), typeof(VMDisplayControl),
                new PropertyMetadata(null, OnCMvdImagePropertyChanged));

        private CMvdImage _image;
        public CMvdImage Image
        {
            set
            {
                if (_image != value)
                {
                    _image = value;
                    SetValue(ImageProperty, value);
                }
                else
                {
                    CMvdImage oldValue = _image;
                    _image = value;
                    SetValue(ImageProperty, value);
                    // 手动触发属性改变通知
                    OnCMvdImagePropertyChanged(this, new DependencyPropertyChangedEventArgs(ImageProperty, oldValue, _image));
                }
            }
            get
            {
                if (!Dispatcher.CheckAccess()) // 如果不在 UI 线程，切换到 UI 线程获取值
                {
                    return (CMvdImage)Dispatcher.Invoke(() => GetValue(ImageProperty));
                }
                return (CMvdImage)GetValue(ImageProperty); ;
            }
        }

        private static void OnCMvdImagePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            VMDisplayControl control = (VMDisplayControl)d;
            CMvdImage image = e.NewValue as CMvdImage ?? new CMvdImage();

            if (!image.IsEmpty)
            {
                try
                {
                    control.MVDRender.ClearImages();
                    control.MVDRender.ClearShapes();
                    control.MVDRender.LoadImageFromObject(image);
                    control._addedShapes.Clear();
                    control._ImageLoaded = true;
                    if (control.ROI != null)
                    {
                        control.AddShape(control.ROI);
                    }
                    else
                    {
                        control.InitializeROI(control.ROIType);
                    }
                }
                catch
                {
                    control._ImageLoaded = false;
                }
            }
            else
            {
                try
                {
                    control.MVDRender.ClearImages();
                    control.MVDRender.ClearShapes();
                    control._addedShapes.Clear();
                    control._ImageLoaded = false;
                }
                catch
                {
                    control._ImageLoaded = false;
                }
            }
            control.MVDRender.Display();
        }
        /// <summary>
        /// ROI类型
        /// </summary>
        public static readonly DependencyProperty ROITypeProperty =
                DependencyProperty.Register("ROIType",
                    typeof(ROITypeEnum),
                    typeof(VMDisplayControl),
                    new PropertyMetadata(ROITypeEnum.全图, OnROITypeChanged));

        public ROITypeEnum ROIType
        {
            get
            {
                if (!Dispatcher.CheckAccess()) // 检查是否在 UI 线程中
                {
                    return (ROITypeEnum)Dispatcher.Invoke(() => GetValue(ROITypeProperty)); // 如果不在 UI 线程，使用 Dispatcher.Invoke 切换到 UI 线程获取值
                }
                return (ROITypeEnum)GetValue(ROITypeProperty); // 如果在 UI 线程，直接获取值
            }
            set
            {
                if (!Dispatcher.CheckAccess()) // 检查是否在 UI 线程中
                {
                    Dispatcher.Invoke(() => SetValue(ROITypeProperty, value)); // 如果不在 UI 线程，使用 Dispatcher.Invoke 切换到 UI 线程设置值
                }
                else
                {
                    SetValue(ROITypeProperty, value); // 如果在 UI 线程，直接设置值
                }
            }
        }

        private static void OnROITypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var instance = (VMDisplayControl)d;
            var newValue = (ROITypeEnum)e.NewValue;
            instance.InitializeROI(newValue);
        }

        /// <summary>
        /// 
        /// </summary>
        public static readonly DependencyProperty ShowDrawArcProperty =
                DependencyProperty.Register("ShowDrawArc",
                    typeof(bool),
                    typeof(VMDisplayControl),
                    new PropertyMetadata(true, OnShowDrawArcChanged));

        public bool ShowDrawArc
        {
            get
            {
                if (!Dispatcher.CheckAccess()) // 检查是否在 UI 线程中
                {
                    return (bool)Dispatcher.Invoke(() => GetValue(ShowDrawArcProperty)); // 如果不在 UI 线程，使用 Dispatcher.Invoke 切换到 UI 线程获取值
                }
                return (bool)GetValue(ShowDrawArcProperty); // 如果在 UI 线程，直接获取值
            }
            set
            {
                if (!Dispatcher.CheckAccess()) // 检查是否在 UI 线程中
                {
                    Dispatcher.Invoke(() => SetValue(ShowDrawArcProperty, value)); // 如果不在 UI 线程，使用 Dispatcher.Invoke 切换到 UI 线程设置值
                }
                else
                {
                    SetValue(ShowDrawArcProperty, value); // 如果在 UI 线程，直接设置值
                }
            }
        }

        private static void OnShowDrawArcChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var instance = (VMDisplayControl)d;
            var newValue = (bool)e.NewValue;
            if (instance._DrawArc != null)
            {
                instance.UpdateDrawArcVisibility();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static readonly DependencyProperty ShowOutlineProperty =
                DependencyProperty.Register("ShowOutline",
                    typeof(bool),
                    typeof(VMDisplayControl),
                    new PropertyMetadata(true, OnShowOutlineChanged));

        public bool ShowOutline
        {
            get
            {
                if (!Dispatcher.CheckAccess()) // 检查是否在 UI 线程
                {
                    return (bool)Dispatcher.Invoke(() => GetValue(ShowOutlineProperty)); // 如果不在 UI 线程，切换到 UI 线程获取值
                }
                return (bool)GetValue(ShowOutlineProperty); // 如果在 UI 线程，直接获取值
            }
            set
            {
                if (!Dispatcher.CheckAccess()) // 检查是否在 UI 线程
                {
                    Dispatcher.Invoke(() => SetValue(ShowOutlineProperty, value)); // 如果不在 UI 线程，切换到 UI 线程设置值
                }
                else
                {
                    SetValue(ShowOutlineProperty, value); // 如果在 UI 线程，直接设置值
                }
            }
        }


        private static void OnShowOutlineChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var instance = (VMDisplayControl)d;
            var newValue = (bool)e.NewValue;
            instance.UpdateOutlinesVisibility();
        }

        /// <summary>
        /// 是否显示矩形框列表
        /// </summary>
        public static readonly DependencyProperty ShowRectangleFListProperty =
                DependencyProperty.Register("ShowRectangleFList",
                    typeof(bool),
                    typeof(VMDisplayControl),
                    new PropertyMetadata(true, OnShowRectangleFListChanged));

        public bool ShowRectangleFList
        {
            get
            {
                // 如果当前线程不是 UI 线程，使用 Dispatcher.Invoke 调用 UI 线程上的 GetValue 方法
                if (!Dispatcher.CheckAccess())
                {
                    return (bool)Dispatcher.Invoke(() => GetValue(ShowRectangleFListProperty));
                }

                return (bool)GetValue(ShowRectangleFListProperty);
            }
            set
            {
                if (!Dispatcher.CheckAccess())
                {
                    // 如果当前线程不是 UI 线程，使用 Dispatcher.Invoke 调用 UI 线程上的 SetValue 方法
                    Dispatcher.Invoke(() => SetValue(ShowRectangleFListProperty, value));
                }
                else
                {
                    SetValue(ShowRectangleFListProperty, value);
                }
            }
        }


        private static void OnShowRectangleFListChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var instance = (VMDisplayControl)d;
            var newValue = (bool)e.NewValue;
            instance.UpdateRectangleFVisibility();
        }

        /// <summary>
        /// 是否显示匹配框列表
        /// </summary>
        public static readonly DependencyProperty ShowMatchOutlineProperty =
                DependencyProperty.Register("ShowMatchOutline",
                    typeof(bool),
                    typeof(VMDisplayControl),
                    new PropertyMetadata(true, OnShowMatchOutlineChanged));

        public bool ShowMatchOutline
        {
            get
            {
                if (!Dispatcher.CheckAccess())
                {
                    return (bool)Dispatcher.Invoke(() => GetValue(ShowMatchOutlineProperty));
                }

                return (bool)GetValue(ShowMatchOutlineProperty);
            }
            set
            {
                if (!Dispatcher.CheckAccess())
                {
                    Dispatcher.Invoke(() => SetValue(ShowMatchOutlineProperty, value));
                }
                else
                {
                    SetValue(ShowMatchOutlineProperty, value);
                }
            }
        }


        private static void OnShowMatchOutlineChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var instance = (VMDisplayControl)d;
            var newValue = (bool)e.NewValue;
            instance.UpdateCPatMatchOutlineVisibility();
        }
        //匹配点
        public CMvdPointSetF cMvdPoint = new CMvdPointSetF();
        /// <summary>
        /// ROI 数据
        /// </summary>
        public static readonly DependencyProperty ROIProperty =
                DependencyProperty.Register("ROI",
                    typeof(CMvdShape),
                    typeof(VMDisplayControl),
                    new PropertyMetadata(null, OnROIChanged));

        public CMvdShape? ROI
        {
            get
            {
                if (!Dispatcher.CheckAccess()) // 检查是否是 UI 线程
                {
                    // 如果不是 UI 线程，使用 Dispatcher.Invoke 切换到 UI 线程
                    return (CMvdShape?)Dispatcher.Invoke(() => GetValue(ROIProperty));
                }
                return (CMvdShape?)GetValue(ROIProperty);
            }
            set
            {
                if (!Dispatcher.CheckAccess()) // 检查是否是 UI 线程
                {
                    // 如果不是 UI 线程，使用 Dispatcher.Invoke 切换到 UI 线程
                    Dispatcher.Invoke(() => SetValue(ROIProperty, value));
                }
                else
                {
                    SetValue(ROIProperty, value);
                }
            }
        }

        private static void OnROIChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var instance = (VMDisplayControl)d;
            var oldROI = (CMvdShape?)e.OldValue;
            var newROI = (CMvdShape?)e.NewValue;

            if (oldROI != null)
            {
                instance.RemoveShape(oldROI);
            }

            instance.PositionFixROI = null; // Reset PositionFixROI when ROI changes

            if (newROI != null)
            {
                newROI.Interaction = true;
                newROI.BorderColor = new MVD_COLOR(0, 255, 0, 255);
                newROI.BorderWidth = 5;
                if (instance._ImageLoaded)
                {

                    instance.AddShape(newROI);
                }
            }
        }

        /// <summary>
        /// 位置修正ROI数据
        /// </summary>
        public static readonly DependencyProperty PositionFixROIProperty =
                DependencyProperty.Register("PositionFixROI",
                    typeof(CMvdShape),
                    typeof(VMDisplayControl),
                    new PropertyMetadata(null, OnPositionFixROIChanged));

        public CMvdShape? PositionFixROI
        {
            get
            {
                if (!Dispatcher.CheckAccess()) // 检查是否在 UI 线程中
                {
                    return (CMvdShape?)Dispatcher.Invoke(() => GetValue(PositionFixROIProperty)); // 如果不在 UI 线程，使用 Dispatcher.Invoke 以切换到 UI 线程
                }
                return (CMvdShape?)GetValue(PositionFixROIProperty);
            }
            set
            {
                if (!Dispatcher.CheckAccess()) // 检查是否在 UI 线程中
                {
                    Dispatcher.Invoke(() => SetValue(PositionFixROIProperty, value)); // 如果不在 UI 线程，使用 Dispatcher.Invoke 切换到 UI 线程
                }
                else
                {
                    SetValue(PositionFixROIProperty, value);
                }
            }
        }

        private static void OnPositionFixROIChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var instance = (VMDisplayControl)d;
            var oldPositionFixROI = (CMvdShape?)e.OldValue;
            var newPositionFixROI = (CMvdShape?)e.NewValue;

            if (instance.ROI != null)
            {
                instance.RemoveShape(instance.ROI);
            }

            if (oldPositionFixROI != null)
            {
                instance.RemoveShape(oldPositionFixROI);
            }

            if (newPositionFixROI != null)
            {
                newPositionFixROI.Interaction = true;
                newPositionFixROI.BorderColor = new MVD_COLOR(0, 255, 0, 255);
                newPositionFixROI.BorderWidth = 5;
                instance.AddShape(newPositionFixROI);
            }
        }


        #endregion
        #region 属性
        // 海康显示控件
        private MVDRenderActivex _MVDRender;
        public MVDRenderActivex MVDRender
        {
            get
            {
                try
                {
                    _MVDRender ??= new MVDRenderActivex();
                    return _MVDRender;
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }


        // 图片正常加载
        public bool _ImageLoaded = false;
        // 轮廓点放大倍数
        public Int32 _Scale = 3;
        public CMvdAnnularSectorF _DrawArc;                                                     // 绘制圆弧
        public List<CMvdLineSegmentF> _DrawOutlineList = new List<CMvdLineSegmentF>();          // 绘制边缘点
        public List<CMvdRectangleF> _DrawRectangleFList = new List<CMvdRectangleF>();           // 绘制矩形框
        private List<CMvdPointSetF> _MatchOutlineList = new List<CMvdPointSetF>();              // 匹配轮廓列表
        public ObservableCollection<CMvdShape> _addedShapes = new ObservableCollection<CMvdShape>();  // 已经绘制添加的图形

        #endregion
        /// <summary>
        /// 构造函数
        /// </summary>
        public VMDisplayControl()
        {
            InitializeComponent();

            if (MVDRender != null)
            {
                winFormHost.Child = MVDRender;
                //设置渲染控件背景色
                MVDRender.BackColor = Color.FromArgb(0, 0, 0, 0);
                //设置切换同分辨率图像保留图形
                MVDRender.EnableRetainShape = true;
                //设置菜单语言为中文
                MVDRender.MenuLanguage = MVDRenderMenuLangType.Chinese;
                //设置交互模式，标准交互+自定义交互
                MVDRender.InteractType = MVDRenderInteractType.StandardAndCustom;
                MVDRender.SetConfiguration((uint)MVD_RENDER_PARAM_KEY.MvdRenderSmoothingMode, (int)MVD_RENDER_SMOOTHING_MODE.MvdSmoothingModeHighQuality);

                //删除矩形菜单项，右键菜单中对应项会被删除
                MVDRender.SetMenuState(System.Convert.ToUInt32(MVD_MENU_ID.MvdCircle), System.Convert.ToUInt32(MVD_MENU_CMD.MvdMenuDelete), null);
                //MVDRender.SetMenuState(System.Convert.ToUInt32(MVD_MENU_ID.MvdText), System.Convert.ToUInt32(MVD_MENU_CMD.MvdMenuDelete), null);
                MVDRender.SetMenuState(System.Convert.ToUInt32(MVD_MENU_ID.MvdLineSegment), System.Convert.ToUInt32(MVD_MENU_CMD.MvdMenuDelete), null);
                MVDRender.SetMenuState(System.Convert.ToUInt32(MVD_MENU_ID.MvdRectangle), System.Convert.ToUInt32(MVD_MENU_CMD.MvdMenuDelete), null);
                MVDRender.SetMenuState(System.Convert.ToUInt32(MVD_MENU_ID.MvdPolygon), System.Convert.ToUInt32(MVD_MENU_CMD.MvdMenuDelete), null);
                MVDRender.SetMenuState(System.Convert.ToUInt32(MVD_MENU_ID.MvdParallelogram), System.Convert.ToUInt32(MVD_MENU_CMD.MvdMenuDelete), null);
                MVDRender.SetMenuState(System.Convert.ToUInt32(MVD_MENU_ID.MvdAnnularSector), System.Convert.ToUInt32(MVD_MENU_CMD.MvdMenuDelete), null);
                MVDRender.SetMenuState(System.Convert.ToUInt32(MVD_MENU_ID.MvdZoom), System.Convert.ToUInt32(MVD_MENU_CMD.MvdMenuDelete), null);
                MVDRender.SetMenuState(System.Convert.ToUInt32(MVD_MENU_ID.MvdRotate), System.Convert.ToUInt32(MVD_MENU_CMD.MvdMenuDelete), null);
                MVDRender.SetMenuState(System.Convert.ToUInt32(MVD_MENU_ID.MvdEraser), System.Convert.ToUInt32(MVD_MENU_CMD.MvdMenuDelete), null);
                MVDRender.SetMenuState(System.Convert.ToUInt32(MVD_MENU_ID.MvdMainMenuPaste), System.Convert.ToUInt32(MVD_MENU_CMD.MvdMenuDelete), null);
                //  MVDRender.MVDShapeChangedEvent += MVDRender_MVDShapeChangedEvent;
                MVDRender.Display();


            }
        }

        #region 事件

        /// <summary>
        /// 触发事件
        /// </summary>
        /// <param name="enEventType"></param>
        /// <param name="enShapeType"></param>
        /// <param name="cShapeObj"></param>
        public void MVDRender_MVDShapeChangedEvent
            (
            MVDRenderActivex.MVD_SHAPE_EVENT_TYPE enEventType,
            MVD_SHAPE_TYPE enShapeType,
            CMvdShape cShapeObj
            )
        {
            if (MVDRenderActivex.MVD_SHAPE_EVENT_TYPE.MVD_SHAPE_SELECTED == enEventType)
            {
                return;
            }
            if (MVDRenderActivex.MVD_SHAPE_EVENT_TYPE.MVD_SHAPE_EDITED == enEventType)
            {
                return;
            }

            /* 图形增加事件 */
            if (MVDRenderActivex.MVD_SHAPE_EVENT_TYPE.MVD_SHAPE_ADDED == enEventType)
            {
                AddShape(cShapeObj);
            }
        }


        #endregion
        #region 方法
        /// <summary>
        /// 添加图形
        /// </summary>
        /// <param name="shape"></param>
        public void AddShape(CMvdShape? shape)
        {
            if (shape == null)
            {
                return;
            }
            if (!_addedShapes.Contains(shape))
            {
                MVDRender.AddShape(shape);
                _addedShapes.Add(shape);
            }
            else
            {
                MVDRender.UpdateShapeInfo(ref shape, true);
            }
        }

        /// <summary>
        /// 删除图形
        /// </summary>
        /// <param name="shape"></param>
        public void RemoveShape(CMvdShape? shape)
        {
            if (shape == null)
            {
                return;
            }
            if (_addedShapes.Contains(shape))
            {
                MVDRender.DeleteShape(shape);
                _addedShapes.Remove(shape);
            }
        }

        /// <summary>
        /// 清理所有结果
        /// </summary>
        public void Clear()
        {
            if (Image == null)
            {
                return;
            }
            // 创建 _addedShapes 的副本
            var shapesToRemove = _addedShapes.ToList();

            foreach (CMvdShape shape in shapesToRemove)
            {
                if (shape != ROI)
                {
                    RemoveShape(shape);
                }
            }
            _DrawOutlineList.Clear();
            _DrawRectangleFList.Clear();
            _MatchOutlineList.Clear();
            MVDRender.Display();
        }

        /// <summary>
        /// 绘制圆弧
        /// </summary>
        public void DrawArc(CMvdAnnularSectorF AnnularSectorF)
        {
            RemoveShape(_DrawArc);
            _DrawArc = AnnularSectorF;
            AddShape(_DrawArc);
            UpdateDrawArcVisibility();
        }

        /// <summary>
        /// 更新圆弧状态
        /// </summary>
        private void UpdateDrawArcVisibility()
        {
            if (ShowDrawArc)
            {
                AddShape(_DrawArc);
            }
            else
            {
                RemoveShape(_DrawArc);
            }
            MVDRender.Display();
        }

        /// <summary>
        /// 绘制矩形框图形（卡尺）
        /// </summary>
        /// <param name="caliperBoxList"></param>
        public void DrawRectangleFList(List<CMvdRectangleF> RectangleFList)
        {
            foreach (var item in RectangleFList)
            {
                _DrawRectangleFList.Add(item);
            }
            UpdateRectangleFVisibility();
        }

        /// <summary>
        /// 更新卡尺显示状态
        /// </summary>
        private void UpdateRectangleFVisibility()
        {
            foreach (var item in _DrawRectangleFList.ToList())
            {
                if (ShowRectangleFList)
                {
                    AddShape(item);
                }
                else
                {
                    RemoveShape(item);
                }
            }
            MVDRender.Display();
        }

        /// <summary>
        /// 绘制边缘点
        /// </summary>
        /// <param name="drawEdgePointList"></param>
        public void DrawOutline(List<CMvdLineSegmentF> LineSegmentFList)
        {
            foreach (var item in LineSegmentFList)
            {
                _DrawOutlineList.Add(item);

            }
            UpdateOutlinesVisibility();
        }

        /// <summary>
        /// 更新轮廓显示状态
        /// </summary>
        private void UpdateOutlinesVisibility()
        {
            foreach (var outline in _DrawOutlineList)
            {
                if (ShowOutline)
                {
                    AddShape(outline);
                }
                else
                {
                    RemoveShape(outline);
                }
            }
            MVDRender.Display();
        }

        /// <summary>
        /// 绘制点集（模板轮廓）
        /// </summary>
        public void DrawPointSetFList(List<CMvdPointSetF> PointSetFList)
        {
            foreach (var item in PointSetFList)
            {
                _MatchOutlineList.Add(item);
            }
            this.UpdateCPatMatchOutlineVisibility();
        }

        /// <summary>
        /// 更新模板轮廓
        /// </summary>
        private void UpdateCPatMatchOutlineVisibility()
        {
            foreach (var outline in _MatchOutlineList)
            {
                if (0 != outline.PointsList.Count)
                {
                    if (ShowMatchOutline)
                    {
                        AddShape(outline);
                    }
                    else
                    {
                        RemoveShape(outline);
                    }
                }
            }
            MVDRender.Display();
        }
        #endregion
        #region  ROI

        public void InitializeROI(ROITypeEnum roiType)
        {


            if (_ImageLoaded)
            {
                // 获取图像尺寸
                var width = _image.Width;
                var height = _image.Height;
                MVD_SHAPE_TYPE mVD_SHAPE_TYPE = MVD_SHAPE_TYPE.MvdShapeUndefined;
                if (cMvdPoint.PointsList.Count <= 1)
                {
                    cMvdPoint.AddPoint(width / 2, height / 2);
                }



                if (roiType == ROITypeEnum.全图)
                {
                    RemoveShape(ROI);
                    ROI = null;

                }
                else if (roiType == ROITypeEnum.矩形)
                {
                    if (ROI is not CMvdRectangleF)
                    {
                        RemoveShape(ROI);
                        // 初始化矩形 ROI
                        ROI = new CMvdRectangleF(width / 2, height / 2, width / 2, height / 2); // 示例尺寸
                        mVD_SHAPE_TYPE = MVD_SHAPE_TYPE.MvdShapeRectangle;

                    }
                }
                else if (roiType == ROITypeEnum.圆环)
                {
                    // 移除现有的 ROI（如果有）
                    if (ROI is not CMvdAnnularSectorF)
                    {
                        RemoveShape(ROI);
                        // 初始化圆环 ROI
                        MVD_POINT_F center = new MVD_POINT_F { fX = width / 2, fY = height / 2 };
                        float innerRadius = Math.Min(width, height) / 8; // 根据需要调整
                        float outerRadius = innerRadius * 2;
                        ROI = new CMvdAnnularSectorF(center, innerRadius, outerRadius, -180f, 360f);
                        mVD_SHAPE_TYPE = MVD_SHAPE_TYPE.MvdShapeAnnularSector;

                    }
                }
                else if (roiType == ROITypeEnum.圆)
                {
                    // 移除现有的 ROI（如果有）
                    if (ROI is not CMvdCircleF)
                    {
                        RemoveShape(ROI);
                        // 初始化圆 ROI
                        MVD_POINT_F center = new MVD_POINT_F { fX = width / 2, fY = height / 2 };
                        float Radius = Math.Min(width, height) / 4; // 根据需要调整
                        ROI = new CMvdCircleF(center, Radius);
                        mVD_SHAPE_TYPE = MVD_SHAPE_TYPE.MvdShapeCircle;
                    }
                }


                if (ROI != null)
                {
                    ROI.Interaction = true;
                    ROI.BorderColor = new MVD_COLOR(0, 255, 0, 255);
                    ROI.BorderWidth = 5;
                    // 手动触发图形添加事件

                    AddShape(ROI);
                }
                //cMvdPoint.Interaction = true;
                //cMvdPoint.BorderColor = new MVD_COLOR(0, 255, 0, 255);
                //cMvdPoint.BorderWidth = 10;
                //AddShape(cMvdPoint);
                MVDRender.Display();

            }
        }

        #endregion
        #region

        public void Copy(VMDisplayControl vMDisplay)
        {
            this.Dispatcher.Invoke(() =>
            {
                this.Clear();
                this.Image = vMDisplay.Image;
                foreach (var shape in vMDisplay._addedShapes)
                {
                    AddShape(shape);
                }
                MVDRender.Display();
            });
        }

        public void Dispose()
        {
            // 释放托管资源
            if (MVDRender != null)
            {
                MVDRender.Dispose(); // 调用控件的Dispose方法释放控件资源
            }
            GC.SuppressFinalize(this); // 防止对象被重复调用析构函数
        }

        #endregion
    }
}
